package com.site.wisdom_construction.controller.userAuthorityModule;

import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.userAuthorityModule.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/3/4
 * @Description
 */
@Api(tags = "用户账号接口")
@RestController
@RequestMapping("User")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "分页模糊查询用户信息", notes = "测试数据:")
    @PostMapping("/findUserByProjectId")
    public Page findUserByProjectId(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(userService.Total(map));
        page.setItems(userService.findUserByProjectId(map));
        return page;
    }

    @ApiOperation(value = "增加用户账号信息", notes = "测试数据:")
    @PostMapping("/insertUser")
    public JsonResult insertUser(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = userService.insertUser(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入用户账号!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入用户账号失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除用户账户信息", notes = "测试数据:")
    @PostMapping("/deleteUser")
    public JsonResult deleteUser(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = userService.deleteUser(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除用户账户!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除用户账户失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改用户账户", notes = "测试数据:")
    @PostMapping("/updateUser")
    public JsonResult updateUser(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = userService.updateUser(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改用户账户!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改用户账户失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改用户密码", notes = "测试数据:")
    @PostMapping("/updateUserPassword")
    public JsonResult updateUserPassword(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = userService.updateUserPassword(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改用户账户密码!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改用户账户密码失败!");
        }
        return jsonResult;
    }


}
